package megrez.keycloak.providers.otp;

import java.util.Locale;

import lombok.experimental.UtilityClass;
import megrez.keycloak.common.Consts;
import megrez.keycloak.common.SmsMessage;
import megrez.keycloak.providers.mqtt.MqttUtils;

/**
 * 工具
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@UtilityClass
public class SmsUtils {

    /**
     * 创建邮件消息
     * 
     * @param code 验证码
     * @param ttl  过期时间
     * @param mail 邮件地址
     * @return
     */
    public SmsMessage buildSmsMessage(String code, int ttl, String mobileNumber, Locale locale) {
        SmsMessage rslt = new SmsMessage();
        rslt.setLocale(locale);
        rslt.addPhoneNumber(mobileNumber);
        rslt.addParam("code", code).addParam("ttl", String.valueOf(Math.floorDiv(ttl, 60)));
        rslt.setTemplateCode(Consts.SMS_TEMPLATE_CODE_OTP);
        return rslt;
    }

    public String getMqttTopic(final String realmId) {
        String topic =  Consts.MQTT_TOPIC_PREFIX_SMS + "/" + realmId;
        return MqttUtils.normalizeTopic(topic).toUpperCase();
    }
}
